package com.crishne.anwo.controller;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.crishne.anwo.constant.JsonStringConstant;
import com.crishne.anwo.dto.Pager;
import com.crishne.anwo.model.Country;
import com.crishne.anwo.service.CountryService;
import com.crishne.anwo.utill.CommonUtill;

@Controller
@RequestMapping("sysmgr/country/")
public class CountryController {
	
	@Autowired
	private CountryService countryService;
	
	private CommonUtill commonUtill = new CommonUtill();
	
	
	@RequestMapping("getCountryPageList")
	public void getCountryPageList(HttpServletResponse response,HttpServletRequest request){
		//获取当前页 和 一页显示的数据数目
		String conCurrentPage = request.getParameter("currentpage");
		String stPageSize = request.getParameter("pageSize");
		String stCountryName = request.getParameter("countryName");
		Integer currentpage = 1;
		Integer pageSize = 10;
		if(conCurrentPage != null && !"".equals(conCurrentPage)){
			currentpage = Integer.valueOf(conCurrentPage);
		}
		
		if(stPageSize != null && !"".equals(stPageSize)){
			pageSize = Integer.valueOf(stPageSize);
		}
		
		
		Country searchCountry = new Country();
		searchCountry.setStSimpleName(stCountryName);
		
		Pager countryPageList = countryService.findPageList(searchCountry, currentpage, pageSize);
		JSONObject jsonString = JsonStringConstant.getSuccessMessage(countryPageList);

		commonUtill.OutputJson(jsonString, response, request);
    }
	
	@RequestMapping("getCountryEdit")
	public void  getCountryEdit(HttpServletResponse response,HttpServletRequest request)
	{
		String countryId = request.getParameter("countryId");
		Country tCountry = countryService.getById(countryId);
		
		JSONObject jsonString = JsonStringConstant.getSuccessMessage(tCountry);
		commonUtill.OutputJson(jsonString,response,request);
	}
    
    @RequestMapping("saveOrEditCountry")
	public void  saveOrEditCountry(HttpServletResponse response,HttpServletRequest request){
		//获取参数
    	String stId = request.getParameter("stId");
		String stCode = request.getParameter("stCode");
		String stName = request.getParameter("stName");
		String stSimpleName = request.getParameter("stSimpleName");
		String stPre = request.getParameter("stPre");
		String stFlag = request.getParameter("stFlag");
		String nmOrder = request.getParameter("nmOrder");
		
		Country tCountry = null;
		if(null == stId || "".equals(stId)){
			tCountry = new Country();
		}
		else{
			tCountry = countryService.getById(stId);
		}
		
		tCountry.setStCode(stCode);
		tCountry.setStName(stName);
		tCountry.setStSimpleName(stSimpleName);
		tCountry.setStPre(stPre);
		tCountry.setStFlag(stFlag);
		if(null != nmOrder && !"".equals(nmOrder)){
			tCountry.setNmOrder(Integer.valueOf(nmOrder));
		}
		else{
			tCountry.setNmOrder(0);
		}
		
		
		if(null == stId || "".equals(stId)){
			tCountry.setStId(UUID.randomUUID().toString());
			countryService.save(tCountry);
		}
		else{
			countryService.update(tCountry);
		}
		
		JSONObject jsonString = JsonStringConstant.getNoDataSuccessMessage("保存成功！");

		commonUtill.OutputJson(jsonString, response, request);
    }
    
    @RequestMapping("deleteCountry")
    public void deleteCountry(HttpServletResponse response,HttpServletRequest request){
    	String stIds = request.getParameter("stIds");
    	JSONObject jsonString = new JSONObject();
    	try{
    		if(null != stIds && !"".equals(stIds)){
    			String[] ids = stIds.split(",");
    			for(int i=0 ;i<ids.length;i++){
    				countryService.delete(ids[i]);
    			}
        		jsonString = JsonStringConstant.getNoDataSuccessMessage("删除成功！");
    		}
    		else{
    			jsonString = JsonStringConstant.getNoDataSuccessMessage("删除失败！");
    		}
    	}
    	catch(Exception ex){
    		jsonString = JsonStringConstant.getNoDataSuccessMessage("删除失败！");
    	}
    	
		commonUtill.OutputJson(jsonString, response, request);
    }

}